﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Alogient.Cameleon.Community.Blog.Items
{
    public class TagCloud : List<TagCloudItem>
    {
        private TagCloud() { }

        // TODO Load

        public TagCloud(IDictionary<string, int> keyValues)
        {
            int min = int.MaxValue;
            int max = int.MinValue;

            foreach (KeyValuePair<string, int> pair in keyValues)
            {
                TagCloudItem item = new TagCloudItem();
                item.Name = pair.Key;
                item.Count = pair.Value;

                Add(item);

                if (item.Count > max)
                    max = item.Count;

                if (item.Count < min)
                    min = item.Count;
            }

            int diff = ((max - min) / 3);

            foreach (TagCloudItem item in this)
            {
                if (item.Count == max) { item.Size = 1; }
                else if (item.Count == min) { item.Size = 5; }
                else if (item.Count >= (min + diff * 2)) { item.Size = 2; }
                else if (item.Count >= (min + diff)) { item.Size = 3; }
                else { item.Size = 4; }
            }

            Sort();
        }
    }

    public class TagCloudItem : IComparable<TagCloudItem>
    {
        public string Name;
        public int Count;
        public int Size;


        #region IComparable<TagCloudItem> Members

        public int CompareTo(TagCloudItem other)
        {
            return this.Name.CompareTo(other.Name);
        }

        #endregion
    }
}
